package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.DetailBean.FhName;

import java.util.ArrayList;

public class SendAddressAdapter extends BaseAdapter {

    LayoutInflater inflater;
    Context context;
    ArrayList<FhName> fhnameList;

    public SendAddressAdapter(Context context) {
        this.context = context;
        this.fhnameList = MyApplication.getInstance().getFhnamelist();
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return fhnameList != null ? fhnameList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return fhnameList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        holder = new ViewHolder();
        convertView = inflater
                .inflate(R.layout.ocoffee_send_address_item, null);

        FhName fhName = fhnameList.get(position);

        holder.send_address_tv = (TextView) convertView
                .findViewById(R.id.send_address_name);
        holder.send_address_tv.setText(fhName.getSend_mothod());

        if (fhName.isSelect()) {
            holder.send_address_tv
                    .setBackgroundResource(R.drawable.detail_radio_select);
        } else {
            holder.send_address_tv
                    .setBackgroundResource(R.drawable.detail_radio_unselect);
        }

        if (convertView.getTag() == null) {
            convertView.setTag(holder);
        }
        return convertView;
    }

    public void chooceState(int position) {

        try {
            for (int i = 0; null != MyApplication.getInstance().getFhnamelist() && i < MyApplication.getInstance().getFhnamelist()
                    .size(); i++) {
                MyApplication.getInstance().getFhnamelist().get(i)
                        .setSelect(false);
            }
            MyApplication.getInstance().getFhnamelist().get(position)
                    .setSelect(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class ViewHolder {
        TextView send_address_tv;
    }

}
